package com.qf.rabbitmq.pubsub;

import com.qf.rabbitmq.util.ConnectionUtil;
import com.rabbitmq.client.*;

import java.io.IOException;

//消费者
public class Consumer2 {


    //    声明了一个 队列名
    private static final String QUEUENAME="bk0222";
    //    声明了一个 交换机
    private static final String EXCHANGENAME="2102fanout";


    public static void main(String[] args) throws Exception {
//        获取连接
        Connection connection = ConnectionUtil.getConnection();
//        声明通道
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUEUENAME, false, false, false, null);

        //      绑定消费者到交换机上
        channel.queueBind(QUEUENAME, EXCHANGENAME, "");


        channel.basicConsume(QUEUENAME,true,new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//                这个方法是 处理 收到消息的方法
                System.out.println("消费者2收到的消息是："+new String(body));
            }
        });


    }
}
